package com.infinitegroup.findmystuff.presenters;

import android.content.Context;

import com.infinitegroup.findmystuff.models.*;
import com.infinitegroup.findmystuff.views.RegistrationView;

public class RegistrationPresenter {
	
	private RegistrationView view;
	private Account account;
	
	public RegistrationPresenter(RegistrationView view){
		this.view = view;
		account = new AccountLink("", (Context) view); //empty string as we're adding a new user.	
	}
	

	public void handleRegisterClick(String firstName, String lastName,
			String username, String password, String email, String phone,
			String zipCode) {
		
		if (!(firstName.matches("[A-Za-z]+"))){
			view.displayMessage("Please enter a valid first name. First letter must be capitalized");
			return;
		}
		if (!(lastName.matches("[A-Za-z]+"))){
			view.displayMessage("Please enter a valid last name. First letter must be capitalized");
			return;
		}
		if (!(username.matches("[A-Za-z]+(A-Za-z0-9._-)*"))){
			view.displayMessage("Please enter a valid username that may only contain lowercase and uppercase letters, numbers, periods, underscores, or dashes.");
			return;
		}
		if (password.length()==0){
			view.displayMessage("Please enter a valid password.");
			return;
		}
		if (!(email.matches("[A-Za-z]+[A-Za-z0-9._-]*(@)[A-Za-z0-9_-]+\\w*[.]+[A-Za-z0-9]+"))){
			view.displayMessage("Please enter a valid email address.");
			return;
		}
		if (!(phone.matches("[0-9]{10}"))){
			view.displayMessage("Please enter a valid phone number in the form ##########.");
			return;
		}
		if (!(zipCode.matches("[0-9]{5}"))){
			view.displayMessage("Please enter a valid 5 digit zip code.");
			return;
		}
		
		//register the user.
		if (!account.exists(username)){ // register if account does not exist.
			System.out.println("id is "+account.register(firstName, lastName, username, password, email, phone, zipCode));
			view.displayMessage("Account Registered");
			view.advanceToLogin();
		} else {
			view.displayMessage("Username already exists");
		}
	}
}
